package cn.edu.scau.cmi.zenglinghong.composite.safe;

import java.util.Set;

public class SafeTeam extends SafeComponent{

	public SafeTeam(String name, SafeComponent organization) {
		super(name, organization);
	}
	
	@Override
	public String doSomething() {
		System.out.println("你好，我是团队组织，可以增加新成员。");
		return "你好，我是团队组织，可以增加新成员。";
	}
	
	public SafeComponent addChild(SafeComponent child) {
		children.add(child);
		return this;
	}

	public SafeComponent deleteChild(SafeComponent child) {
		children.remove(child);
		return this;
	}
	
	public Set<SafeComponent> getChildren() {
		return children;
	}
	
	@Override
	public String toString(int depth) {
		String parentName = getParent() == null ? "无" : getParent().getName();
		String blank = "";
		for (int i=0; i<depth; i++) blank+=" ";
		StringBuilder sb = new StringBuilder();
		for (SafeComponent child : children) {
			String str = child.toString(depth + 4);
			str = str.substring(0, str.length()-1);
			str = str + ",\n";
			sb.append(str);
		}
		if (children.size() != 0) {
			sb.deleteCharAt(sb.length()-2);
		}
		String res = blank +"\"" + getName() + "\": {\n" + 
				blank + " \"类型\": \"团队\",\n" +
				blank + " \"父亲\": \"" + parentName + "\",\n" + 
				blank + " \"孩子\": [\n" + 
				sb.toString() + 
				blank + " ]\n" + 
				blank + "}\n";
		return res;
	}
	
	@Override
	public String toString() {
		return toString(0);
	}
}
